package ALMySqlCommon.ALMySqlSafeOp;

import java.sql.SQLDataException;
import java.sql.SQLSyntaxErrorException;
import java.sql.SQLWarning;
import java.util.ArrayList;

import ALMySqlCommon.ALMySqlCommonObj.ALMySqlErrDealer._IALMySqlDBExeErrorDealer;

/****************
 * 默认的数据库操作错误处理对象，以打印为主
 * @author mj
 *
 */
public class ALMySqlSafeOpErrDealer implements _IALMySqlDBExeErrorDealer 
{
	//对应数据库对象
	@SuppressWarnings("rawtypes")
	private _TALMySqlSafeOpDBObj _m_doDBObj;
	//额外的错误处理对象
	private _IALMySqlDBExeErrorDealer _m_edExErrDealer;

	@SuppressWarnings("rawtypes")
	public ALMySqlSafeOpErrDealer(_TALMySqlSafeOpDBObj _dbObj)
	{
		_m_doDBObj = _dbObj;
		_m_edExErrDealer = null;
	}
	@SuppressWarnings("rawtypes")
	public ALMySqlSafeOpErrDealer(_TALMySqlSafeOpDBObj _dbObj, _IALMySqlDBExeErrorDealer _exErrDealer)
	{
		_m_doDBObj = _dbObj;
		_m_edExErrDealer = _exErrDealer;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onException(Exception _ex, String _sqlTxt, ArrayList<byte[]> _binaryList) 
	{
		try
		{
			if(_ex instanceof SQLSyntaxErrorException)
			{
				//语法错误，此时不加入错误队列
		        System.out.print("SQLSyntaxErrorException:'" + _sqlTxt + "' error!!!\n");
		        return ;
			}
			else if(_ex instanceof SQLDataException)
			{
				//数据格式错误，此时不加入队列处理
		        System.out.print("SQLDataException:'" + _sqlTxt + "' error!!!\n");
		        return ;
			}
			else if(_ex instanceof SQLWarning)
			{
				//未知错误，由于主要处理的是数据库连接断开后的错误处理，这里warning也不处理
		        System.out.print("SQLWarning:'" + _sqlTxt + "' error!!!\n");
		        return ;
			}
			
	        System.out.print("SQL:'" + _sqlTxt + "' error!!!\n");
	        _ex.printStackTrace();
	        
	        //将数据放入操作中
	        if(null != _m_doDBObj)
	        	_m_doDBObj.addDealQuery(_sqlTxt, _binaryList);
		}
		finally
		{
			if(null != _m_edExErrDealer)
				_m_edExErrDealer.onException(_ex, _sqlTxt, _binaryList);
		}
	}

}
